<?php

namespace api\libs\event\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{event_log}}".
 *
 * The followings are the available columns in table '{{event_log}}':
 * @property integer $id
 * @property integer $uid
 * @property integer $eventid
 * @property integer $type
 * @property integer $object
 * @property integer $objectid
 * @property integer $datatype
 * @property string $data
 * @property integer $ctime
 * @property integer $mtime
 * @property integer $cuser
 * @property integer $muser
 * @property integer $status
 */
class EventLog extends ActiveRecord {

    CONST TYPE_CAMPAIGN = 1;
    CONST TYPE_ADGROUP = 2;
    CONST TYPE_ADSET = 3;
    CONST TYPE_AD = 4;
    CONST TYPE_CREATIVE_SET = 5;
    CONST TYPE_CREATIVE = 6;
    
    CONST STATUS_SUCCESS = 1;
    CONST STATUS_FAILURE = 0;
    
    CONST TYPE_ARRAY = 1;
    CONST TYPE_SIMPLE = 0;
    
    
    CONST DATA_FIELD_CLIENT = 'client';
    CONST DATA_FIELD_PROJECT = 'project';
    CONST DATA_FIELD_MANAGER = 'manager';

    public static function tableName()
    {
        return 'event_log';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'eventid', 'type', 'objectid', 'datatype', 'status'], 'integer'],
            [['object', 'objectid'], 'required'],
            [['object'], 'string', 'max' => 128],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'eventid' => 'Eventid',
            'type' => 'Type',
            'object' => 'Object',
            'objectid' => 'Objectid',
            'datatype' => 'Datatype',
            'data' => 'Data',
            'status' => 'Status',
            'ctime' => 'Ctime',
            'cuser' => 'Cuser',
            'mtime' => 'Mtime',
            'muser' => 'Muser',
        ];
    }


    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return EventLog the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 模型保存前的预处理，如事件序列化，创建时给创建时间、创建人
     * @return boolean
     */
    
     public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                if (empty($this->ctime)) {
                    $this->ctime = $this->mtime = time();
                }
                if (empty($this->uid)) {
                    $this->uid = Yii::$app->user->id;
                }
            } else {
                $this->mtime = time();
            }
            if (is_array($this->data) && !empty($this->data)) {
                $this->data = serialize($this->data);
                $this->datatype = self::TYPE_ARRAY;
            } else {
                $this->data = !empty($this->data) ? $this->data : '';
                $this->datatype = self::TYPE_SIMPLE;
            }
        }
        return true;
    }


    /**
     * 模型加载前的预处理，反序列化积分日志描述
     */
    public function afterFind() {
        parent::afterFind();
        if ($this->datatype == self::TYPE_ARRAY) {
            $this->data = unserialize($this->data);
        }
    }

    public static function getStatusArray() {
        return array(
            self::STATUS_FAILURE => Yii::t('credits', 'Failure'),
            self::STATUS_SUCCESS => Yii::t('credits', 'Success'),
        );
    }

    /**
     * 获取积分日志的状态
     * @return string
     */
    public function getStatusDisplay() {
        $return = 'N/A';
        $statusArray = self::getStatusArray();
        if (array_key_exists($this->status, $statusArray)) {
            $return = $statusArray[$this->status];
        }
        return $return;
    }

}
